const mysql = require('mysql2/promise');
const {con} = require('./common');
const fs = require('fs'),
      path = require('path');
const moment = require('moment');

// 文章成功发布提示
// 文章发布时间(publish) 文章标题(title) 文章(id) 用户id(user)
async function inform(id){
    const res = {};
    let sql=`SELECT user,id,title,publish FROM user.essay where user = ${id}`;
    let [rows] = await con.query(sql);
    // 更改数据库时间格式
    for(let item of rows){
        let newPublish = moment(item.publish).format('YYYY/MM/DD HH:mm:ss');
        item.publish = newPublish;
    }
    res.data = rows;
    return res;
}

// 新增关注提示
// 用户id 用户的粉丝fanId 关注时间 粉丝名 头像 
async function getFans(id){
    const res = {};
    let sql=`SELECT fan.id,fanId,fanTime,username,image FROM user.user,user.fan WHERE user.id = fan.fanId and fan.id = ${id};`;
    let [rows] = await con.query(sql);
    for(let item of rows){
        let newFanTime = moment(item.fanTime).format('YYYY/MM/DD HH:mm');
        item.fanTime = newFanTime;
    }
    res.data = rows;
    return res;
}

// 点赞消息提示
async function getLike(id){
    const res = {};
    let sql = `select user.like.unid,essay.user,user.like.userId,passageId,likeTime,username,image,title
    from user.like,user.essay,user.user
    where essay.id = user.like.passageId 
    and user.id = user.like.userId
    and essay.user= ${id}; `;
    let [rows] = await con.query(sql);
    for(let item of rows){
        let newlikeTime = moment(item.likeTime).format('YYYY/MM/DD HH:mm');
        item.likeTime = newlikeTime;
    }
    res.data = rows;
    return res;
}
async function getLikeImg(id){
    const res = {};
    let sql = `SELECT * FROM user.essayimage where id = ${id}`;
    let [rows] = await con.query(sql);
    res.data = rows;
    return res;
}

module.exports = {
    getFans,
    inform,
    getLike,
    getLikeImg
};